//
//  XiaoKaiBrand_detailsViewController.m
//  Project_A
//
//  Created by 凯 on 15/7/13.
//  Copyright (c) 2015年 zhoukaidong. All rights reserved.
//

#import "XiaoKaiBrand_detailsViewController.h"
#import "AFNetWorkHandle.h"
#import "XiaoKaiBrand_detailsModel.h"
#import "BaseHeader.h"
#import "XiaoKaiBrand_detailsCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "SecondViewController.h"
#import "XiaoKaiAttackViewController.h"

//品牌详情 Controller

@interface XiaoKaiBrand_detailsViewController ()
<
UICollectionViewDataSource,
UICollectionViewDelegate
>




@property (nonatomic , retain)NSMutableArray *brand_deta_modelarr;

@property (nonatomic , retain)UIImageView *headimage;

@property (nonatomic , retain)UICollectionView *collectionview;

@property (nonatomic , retain)UIButton *backbutton;


@end

@implementation XiaoKaiBrand_detailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self getdata];
}
-(void)getdata
{
    NSString *str1 = @"http://api.liwushuo.com/v2/brands/";
    NSString *str2 = @"/items?limit=50&offset=0";
    NSString *st = [NSString stringWithFormat:@"%@", self.str3];
    NSString *str = [[str1 stringByAppendingString:st]stringByAppendingString:str2];
    self.brand_deta_modelarr = [NSMutableArray array];
    [AFNetWorkHandle GETwithAFNByURL:str completion:^(id result) {
        NSMutableArray *temparr = [[result objectForKey:@"data"]objectForKey:@"items"];
        NSLog(@"%@",temparr);
        for (NSDictionary *dic in temparr) {
            
            XiaoKaiBrand_detailsModel *model = [[XiaoKaiBrand_detailsModel alloc]initWithDic:dic];
            [self.brand_deta_modelarr addObject:model];
         //   NSLog(@"%@",self.brand_deta_modelarr);
            NSLog(@"###########################%@", model.IDstr);
                
            
        }
        [self creatcollectionview];
        [self getbutton];
    }];
    
    
}
-(void)creatcollectionview
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //collectionviewcell大小
    layout.itemSize = CGSizeMake( (SCREEN_WIDTH - 30) / 2  , SCREEN_HEIGHT / 3);
    // 上 左  下  右  间距
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 5, 10);
    //头部区域大小
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT / 3);
    //间隙
    layout.minimumLineSpacing = 5;
    
    self.collectionview = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionview.backgroundColor = [UIColor clearColor];
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    [self.collectionview registerClass:[XiaoKaiBrand_detailsCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.collectionview registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:self.collectionview];
    [self creatNav];
    
}




-(NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.brand_deta_modelarr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XiaoKaiBrand_detailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    XiaoKaiBrand_detailsModel *model = [self.brand_deta_modelarr objectAtIndex:indexPath.row];
    NSLog(@"%@",model.name);
    cell.Model = model;
    NSLog(@"~~~~~~~~~%@", cell.Model.name);
    
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //通过判断kind参数类型 确定是头部区域还是尾部区域
    if (kind == UICollectionElementKindSectionHeader) {
        //从头部区域 重用池中获取f头部对象
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.backgroundColor = [UIColor clearColor];
        self.headimage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 3 - 40,  SCREEN_WIDTH,40)];
        label.font = [UIFont systemFontOfSize:30];
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        [self.headimage setImageWithURL:[NSURL URLWithString:self.headbackimage]];
        label.text = self.headtitle;
        [header addSubview:self.headimage];
        [header addSubview:label];


        return header;
    }
    return nil;
}
-(void)creatNav
{
    UIView *naview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20 )];
    naview.backgroundColor = [UIColor clearColor];
    [self.view addSubview:naview];
    UILabel *Navlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    Navlabel.backgroundColor = [UIColor clearColor];
    Navlabel.text = self.headtitle;
    Navlabel.font = [UIFont systemFontOfSize:22];
    Navlabel.textColor = [UIColor whiteColor];
    Navlabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:Navlabel];
    [self.view bringSubviewToFront:Navlabel];
}
//添加假的返回按钮
-(void)getbutton
{
    self.backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backbutton.backgroundColor = [UIColor clearColor];
    self.backbutton.frame = CGRectMake(10, 25, 32, 32);
    [self.backbutton setBackgroundImage:[UIImage imageNamed:@"iconfont-xiangzuo@2x"] forState:UIControlStateNormal];
    [self.backbutton addTarget:self action:@selector(backaaa) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.backbutton];
    [self.view bringSubviewToFront:self.backbutton];
}
//返回上一页面的button
-(void)backaaa
{
    [self.navigationController popViewControllerAnimated:NO];
    //    NSLog(@"走走走 回家喽");
    
}
//点击cell 跳转下一页面
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    SecondViewController *second = [[SecondViewController alloc]init];
    XiaoKaiBrand_detailsModel *model = [self.brand_deta_modelarr objectAtIndex:indexPath.row];
    second.web = model.IDstr;
    second.labeltext = model.name;
    second.imageName = model.image_urls[0];
    [self.navigationController pushViewController:second animated:YES];
    
//    XiaoKaiAttackViewController *attack = [[XiaoKaiAttackViewController alloc]init];
//    [self.navigationController pushViewController:attack animated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
